﻿Imports Microsoft.Win32
Imports System.IO
Imports System.Windows.Forms
Imports System.Management
Public Class application
    Public Shared Function checkosandend()
        Dim osver = System.Environment.OSVersion.Version.ToString
        Dim osname As String
        If osver >= "6.1.7600" Then
            osname = "7"
        ElseIf osver >= "6.0" Then
            osname = "vista"
        ElseIf osver >= "5.1" Then
            osname = "xp"
            MsgBox("Sorry Sys Optimizer currently don't support Windows XP", MsgBoxStyle.Critical + vbOKOnly)
            End
        Else
            osname = Nothing
            MsgBox("Sorry Sys Optimizer don't support Versions of windows below Windows Vista", MsgBoxStyle.Critical + vbOKOnly)
            End
        End If
        Return Nothing
    End Function
End Class
Public Class clsWMI
    Private obj_Oper As ManagementObjectSearcher
    Private objmgmt As ManagementObject
    Private m_windowsDir As String
    Public Sub New()
        obj_Oper = New ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem")
        For Each Me.objmgmt In obj_Oper.Get
            m_windowsDir = objmgmt("SystemDrive")
        Next
    End Sub
    Public ReadOnly Property Windowsdir()
        Get
            Windowsdir = m_windowsDir
        End Get
    End Property
End Class
Public Class temperaoryfolder
    Public Shared Function GetTempFolder()
        Dim temp As String
        temp = My.Computer.FileSystem.SpecialDirectories.Temp
        Return temp
    End Function
    Public Shared Function GetInternetfilesfolder()
        Dim zlsWMI As New clsWMI
        With zlsWMI
            Dim folder As String
            folder = .Windowsdir
            folder = folder & "\Users" & My.User.Name.Remove(0, My.Computer.Name.Length) & "\AppData\Local\Microsoft\Windows\Temporary Internet Files\Low"
            'folder = My.Computer.FileSystem.SpecialDirectories.Temp
            'folder = folder.Remove(12, folder.Length - 12)
            Return folder
        End With
    End Function
End Class
Public Class CacheFolder
    Public Shared Function GetChromeCahce()
        Dim cachchrome
        Dim klsWMI As New clsWMI
        With klsWMI
            cachchrome = .Windowsdir & "\Users" & My.User.Name.Remove(0, My.Computer.Name.Length) & "\AppData\Local\Google\Chrome\User Data\Default\Cache"
        End With
        Return cachchrome
    End Function
End Class
Public Class retrieval
    Public Shared nodes As Integer
    Public Shared Function Getflsundfolintemp(ByVal treevw As TreeView, ByVal lstbx As ListBox)
        treevw.Nodes.Clear()
        lstbx.Items.Clear()
        For Each tempfl In Directory.GetFiles(temperaoryfolder.GetTempFolder)
            lstbx.Items.Add(tempfl)
        Next
        For Each tempfol In Directory.GetDirectories(temperaoryfolder.GetTempFolder)
            lstbx.Items.Add(tempfol)
        Next
        Dim cnt = lstbx.Items.Count()
        treevw.Nodes.Add("Temperory Folder" & " ( " & cnt & " items )")

        For Each tempfil In Directory.GetFiles(temperaoryfolder.GetTempFolder)
            Dim myfile As New FileInfo(tempfil)
            Dim size = Math.Round(myfile.Length / 1024, 0)
            treevw.Nodes(nodes).Nodes.Add("File : " & tempfil & "    " & size & " KB")
        Next
        For Each tempfl In Directory.GetDirectories(temperaoryfolder.GetTempFolder)
            treevw.Nodes(nodes).Nodes.Add("Folder : " & tempfl)
        Next

        Return Nothing
    End Function
    Public Shared Function GetInternetFol(ByVal treevw As TreeView, ByVal lstbx As ListBox)

        lstbx.Items.Clear()
        For Each folded In Directory.GetDirectories(temperaoryfolder.GetInternetfilesfolder)
            For Each filed In Directory.GetFiles(folded)
                lstbx.Items.Add(filed)
                On Error Resume Next
            Next
            For Each fod In Directory.GetDirectories(folded)
                For Each fileinfod In Directory.GetFiles(fod)
                    lstbx.Items.Add(fileinfod)
                    On Error Resume Next
                Next
            Next
        Next
        Dim cnnt = lstbx.Items.Count
        treevw.Nodes.Add("Internet Explorer Files ( " & cnnt & " items )")
        For Each folded In Directory.GetDirectories(temperaoryfolder.GetInternetfilesfolder)
            For Each filed In Directory.GetFiles(folded)
                treevw.Nodes(nodes + 1).Nodes.Add(filed)
                On Error Resume Next
            Next
            For Each fod In Directory.GetDirectories(folded)
                For Each fileinfod In Directory.GetFiles(fod)
                    treevw.Nodes(nodes + 1).Nodes.Add("File : " & fileinfod)
                    On Error Resume Next
                Next
            Next
        Next
        Return Nothing
    End Function
    Public Shared Function GetChromeCache(ByVal treevw As TreeView, ByVal Lstbx As ListBox, ByVal chkbx As CheckBox)
        Lstbx.Items.Clear()
        For Each chrocache In Directory.GetFiles(CacheFolder.GetChromeCahce)
            Lstbx.Items.Add(chrocache)
            On Error Resume Next
        Next
        Dim count = Lstbx.Items.Count
        treevw.Nodes.Add("Chrome Cache : ( " & count & " items )")
        For Each chrocache In Directory.GetFiles(CacheFolder.GetChromeCahce)
            If chkbx.Checked = False Then
                treevw.Nodes(nodes + 1).Nodes.Add("File : " & chrocache)
            Else
                treevw.Nodes(nodes + 2).Nodes.Add("File : " & chrocache)
            End If
        Next
        Return Nothing
    End Function
End Class
Public Class removal
    Public Shared Function removechromecahce(ByVal lstbx As ListBox, ByVal prog As ProgressBar)
        Dim lstitems = lstbx.Items
        For Each items In lstitems
            If prog.Value = 100 Then
                prog.Value = 20
            Else
                prog.Value = +1
            End If
            Try
                File.Delete(items)

            Catch ex As Exception

            End Try
        Next
        prog.Value = 100
        Return Nothing
    End Function
    Public Shared Function RemoveInterExfls(ByVal lstbx As ListBox)
        Dim lstitems = lstbx.Items
        For Each items In lstitems
            'Directory.Delete(items)
            Try
                File.Delete(items)
            Catch ex As Exception
            End Try
        Next
        Return Nothing
    End Function
    Public Shared Function removeflsundfolintemp()
        For Each tempret In Directory.GetFiles(temperaoryfolder.GetTempFolder)
            Try

                Dim fol = tempret
                File.Delete(fol)
                'MsgBox("Success Deleted" & tempret)
            Catch ex As Exception
                'Dialog1.ShowDialog()
            End Try
        Next
        For Each tempdirdel In Directory.GetDirectories(temperaoryfolder.GetTempFolder)
            Try
                Directory.Delete(tempdirdel, True)

            Catch ex As Exception
                'Dialog1.ShowDialog()

            End Try
        Next
        Return Nothing
    End Function
End Class
Public Class Processescls
    Public Shared Function EnumerateProcess(ByVal lstProName As ListView)
        lstProName.Items.Clear()
        Try
            Dim sist As Integer = 0
            Dim searcher As New ManagementObjectSearcher( _
                "root\CIMV2", _
                "SELECT * FROM Win32_Process")

            For Each queryObj As ManagementObject In searcher.Get()
                lstProName.Items.Add(queryObj("Caption"))
                lstProName.Items(sist).SubItems.Add(queryObj("ProcessId"))
                lstProName.Items(sist).SubItems.Add(queryObj("PageFileUsage"))
                sist += 1
            Next
        Catch err As ManagementException
            MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
        End Try
        Return Nothing
    End Function
    Public Shared Function EndProcess(ByVal lstprocesse As ListView)
        Dim i As Integer
        For i = 0 To lstprocesse.SelectedItems.Count = i
            Dim retrun
            retrun = lstprocesse.SelectedItems(i).Text
            Try
                Dim sist As Integer = 0
                Dim searcher As New ManagementObjectSearcher( _
                    "root\CIMV2", _
                    "SELECT * FROM Win32_Process")
                For Each queryObj As ManagementObject In searcher.Get()
                    Try
                        Dim lstprocess = queryObj("Caption")
                        Dim lstid = queryObj("ProcessId")
                        If lstprocess = retrun Then
                            Dim p As Process = Process.GetProcessById(lstid)
                            Try
                                p.Kill()
                                Processescls.EnumerateProcess(lstprocesse)
                            Catch ex As Exception
                                MsgBox("Sorry Process Cannot be ended")
                            End Try
                        End If
                    Catch ex As Exception
                    End Try
                Next
            Catch err As ManagementException
                MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            End Try
        Next
        Return Nothing
    End Function
End Class
